package lwk.thread.demo;

/**
 * 抢火车票DEMO
 *
 * 多个线程同时操作一个对象
 * 发现问题：多个线程同时操作一个资源额情况下，线程不安全，数据紊乱
 */
public class TestTicketThread implements Runnable{

    // 火车票总数
    private int ticketNums = 10;

    @Override
    public void run() {
        while (ticketNums > 0) {
            // 模拟延时
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "---->拿到了第" + ticketNums-- + "张票！！");
        }
    }

    public static void main(String[] args) {
        TestTicketThread ticketThread = new TestTicketThread();

        new Thread(ticketThread,"张三").start();
        new Thread(ticketThread,"李四").start();
        new Thread(ticketThread,"王五").start();

    }

}
